let recommend = require('../api/qq/recommend');
let search = require('../api/qq/search');
let songlist = require('../api/qq/songlist');
let album = require('../api/qq/album');
let singer = require('../api/qq/singer');
let song = require('../api/qq/song');
let lyric = require('../api/qq/lyric');

export async function top_play_list_Q(f){
  //console.log('qq');
  await recommend.playlistU({}).then((r) => {
    let result = []
    //console.log(r)
    for (let playlist of r.data.list) {
      result.push(dealqResult(playlist))
    }
    //console.log("@@@q",result.length)

    f(result)
  })
}
function dealqResult(data){
  //console.log(data)
  let {title:name,content_id:id,cover:imgUrl,username : auth} = data;



  return {
    "name":name,
    "id":id,
    "imgUrl":imgUrl,
    "auth":auth,
    "from":'qq'
  }
}


export async function getQSearch(keywords,pageNo,pageSize,type,f){

  await search.index({key:keywords,pageSize:pageSize,pageNo:pageNo,t:type}).then((r) => {
    //console.log(951753);
    //console.log(r);

    let result;
    if(type === 0 || type === '0'){
      result = dealSearchSongResult(r.data)
      let count = r.data?.total || 0
      result = {v:result,count:count}
    }else if(type === 9 || type === '9'){
      result = dealSearchSingerResult(r.data)
    }else if(type === 2 || type === '2'){
      result = dealSearchPlayListResult(r.data)
    }else if(type === 8 || type === '8'){
      result = dealSearchAlbumResult(r.data)
    }


    f(result)
  })
}
function dealSearchAlbumResult(data){
  //console.log(data)
  let als = data.list || []
  return als.map((r) => {

    return {
      "name":r.albumName,
      "id":r.albumMID,
      "img":r.albumPic,
      "auth":r.singerName,
      "from":'qq'
    }

  })



}
function dealSearchPlayListResult(data){
  //console.log(data)
  let als = data.list || []
  return als.map((r) => {

    return {
      "name":r.dissname,
      "id":r.dissid,
      "imgUrl":r.imgurl,
      "auth":r.creator.name,
      "from":'qq'
    }

  })

}
function dealSearchSongResult(data) {
  let tracks = data.list;

  let r = tracks.map((i) => {
    let ts = i.interval;

    let min = Math.floor(ts / 60);
    let sec = Math.floor(ts - min * 60);
    if (min < 10) {
      min = '0' + min;
    }
    if (sec < 10) {
      sec = '0' + sec;
    }


    return {
      'times': `${min} : ${sec}`,
      'name': i.songname,
      'singer': i.singer[0].name,
      'album': i.albumname,
      'albumId': i.albummid,
      'id': i.songmid,
      'from' : 'qq'
    }
  })
  return r
}
function dealSearchSingerResult(data) {
  //console.log(data)
  let result = data.list || {artistCount:0,artists:[{}]}
  let map = result.map((r) => {

    return {
      id:r.singerMID,
      name: r.singerName,
      img: r.singerPic,
      from:'qq'
    }
  });
  return map

}
export async function getQPlayListDetail(id,f){
  await songlist.index({id:id}).then((r) => {

    //console.log(r)
    let result = dealDetailResult(r.data)

    f(result)
  }).catch(() => {

    return new Promise((resolve, reject) => {
      reject('err');
    })

  })
}
function dealDetailResult(data){
  ////console.log(data)
  let {logo:img,visitnum:playCount,dissname:name,disstid:id,desc:description,nick:user} = data;
  let subscribedCount = ""

  let tags = data.tags.map((i) => {
    return i.name
  })
  let tracks = data.songlist;

  let r = tracks.map((i) => {
    let ts = i.interval;

    let min = Math.floor(ts/60);
    let sec = Math.floor(ts - min*60);
    if(min < 10){
      min = '0' + min;
    }
    if(sec < 10){
      sec = '0' + sec;
    }


    return{
      'times' : `${min} : ${sec}`,
      'name' : i.songname,
      'singer' : i.singer[0].name,
      'album' : i.albumname,
      'albumId' : i.albummid,
      'id' : i.songmid,
      "from":'qq'
    }
  })

  return {
    user:user,
    tags:tags,
    name:name,
    id:id,
    description:description,
    img:img,
    playCount:playCount,
    subscribedCount:subscribedCount,
    list:r,
    "from":'qq'
  }
}
export async function getQAlbumDetail(id,f){
  //console.log('33333');
  await album.songs({id:id}).then((r) => {

    ////console.log({data:r})
    let result = dealAlbumDetailResult(r)
    ////console.log(result)
    f(result)
  }).catch(() => {

    return new Promise((resolve, reject) => {
      reject('err');
    })

  })
}
function dealAlbumDetailResult(data){

  let tracks = data.data.list || [];

  let r = tracks.map((i) => {
    let ts = i.interval;

    let min = Math.floor(ts/60);
    let sec = Math.floor(ts - min*60);
    if(min < 10){
      min = '0' + min;
    }
    if(sec < 10){
      sec = '0' + sec;
    }

    return{
      'times' : `${min} : ${sec}`,
      'name' : i.name,
      'singer' : i.singer[0].name,
      'album' : i.album.name,
      'albumId' : i.album.mid,
      'id' : i.mid,
      "from":'qq'
    }
  })


  return r
}

export async function getQSongsBySinger(id,page,perpage,f){

  await singer.songs({singermid:id,num:perpage,page:page}).then((r) => {
    //console.log(r);
    let result = dealSongBySinger(r.data);
    f(result)
  }).catch(() => {

    return new Promise((resolve, reject) => {
      reject('err');
    })

  })
}
function dealSongBySinger(data){
  let list = data.list;
  let result = list.map((i) => {
    let ts = i.interval;

    let min = Math.floor(ts/60);
    let sec = Math.floor(ts - min*60);
    if(min < 10){
      min = '0' + min;
    }
    if(sec < 10){
      sec = '0' + sec;
    }

    return{
      'times' : `${min} : ${sec}`,
      'name' : i.name,
      'singer' : i.singer[0].name,
      'album' : i.album.name,
      'albumId' : i.album.mid,
      'id' : i.mid,
      "from":'qq'
    }

  })
  return {v:result,count:data.total};
}

export async function getQPlayMusic(id,albumId,f){
  let resource = "";
  await song.urls({id:id}).then((r) => {

    //console.log(r)
    let result = dealPlayMusicResultU(r.data)
    resource = result;
  }).catch((r) => {
    ////console.log(r)
    resource = "";
    return new Promise((resolve, reject) => {
      reject('err');
    })
  })
  let img = ""
  await album.img({id:albumId}).then((r) => {

    //console.log(r)
    let result = dealPlayMusicResult(r)
    img = result
  }).catch(() => {

    return new Promise((resolve, reject) => {
      reject('err');
    })

  })
  //console.log('11111111  ' + img);
  f({resource: resource, img: img})

}
function dealPlayMusicResultU(data){
  ////console.log(data)
  let key = Object.values(data)[0]
  ////console.log(key)
  return key || ""


}
function dealPlayMusicResult(data){
  if(data.errMsg){
    return "";
  }
  return data || "";


}

export async function getQlyric(id,f){
  await lyric.index({songmid:id}).then((r) => {
    //console.log(r)
    let result = dealQlyricResult(r.data)

    f(result)
  }).catch(() => {

    return new Promise((resolve, reject) => {
      reject('err');
    })

  })
}
function dealQlyricResult(data){

  return data.lyric || ''

}
